package com.techsoul.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.techsoul.admin.pojo.vo.CategoryGoodsVo;
import com.techsoul.admin.service.IGoodsService;
import com.techsoul.common.core.constant.ApiConstants;
import com.techsoul.common.core.pojo.vo.CategoryVo;
import com.techsoul.common.core.pojo.vo.UserVo;
import com.techsoul.common.core.service.RedisService;
import com.techsoul.common.core.util.ServletUtils;
import com.techsoul.common.core.util.collection.ListUtils;
import com.techsoul.common.security.service.TokenService;
import com.techsoul.module.goods.pojo.param.SpuSearchParam;
import com.techsoul.module.goods.pojo.vo.SpuVo;
import com.techsoul.module.goods.service.ICategoryService;
import com.techsoul.module.goods.service.ISpuService;
import com.techsoul.module.goods.pojo.vo.SkuVo;
import com.techsoul.module.goods.service.ISkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ISpuService spuService;

    @Autowired
    private ISkuService skuService;

    @Autowired
    private RedisService redisService;

    /**
     * 添加商品访问记录
     * @param id
     */
    @Override
    public void addView(String id) {
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        SpuVo spuVo = spuService.findById(id);
        String key = ApiConstants.GOODS_VIEW_KEY + user.getId();
        //为了保证浏览商品的 唯一性,每次添加前,将list 中该 商品ID去掉,在加入,以保证其浏览的最新的商品在最前面
        String keys = id + ",;;" + spuVo.getImage();
        redisService.lRemove(key, 1, keys);
        //将value push 到该key下的list中
        redisService.lLeftPush(key, keys);
        //使用ltrim将60个数据之后的数据剪切掉
        redisService.lTrim(key, 0, 59);
        // 设置缓存时间为一个月
        redisService.expire(key, 60 * 60 * 24 * 30);
    }

    /**
     * 根据SpuId获取商品详情
     * @param spuId
     * @return
     */
    @Override
    public SpuVo goodsDetailBySpuId(String spuId) {
        return  this.spuService.findById(spuId);
    }

    /**
     * 根据SkuId获取商品详情
     * @param skuId
     * @return
     */
    @Override
    public SkuVo goodsDetailBySkuId(String skuId) {
        return this.skuService.findById(skuId);
    }


    /**
     * 商品列表-分页
     * @param params
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SpuVo> search(SpuSearchParam params, Integer current, Integer pageSize) {
        return spuService.findAll(params, current, pageSize);
    }

    /**
     * 分类商品列表
     * @return
     */
    @Override
    public List<CategoryGoodsVo> categoryGoodsList() {
        // 优先返回缓存数据
        List<CategoryGoodsVo> result = redisService.getCacheList(ApiConstants.CATEGORY_GOODS_KEY);
        if (ListUtils.isNotEmpty(result)) {
            return result;
        } else {
            result = ListUtils.newArrayList();
        }
        // 2级以上分类
     /*   List<CategoryVo> categoryList = categoryService.findLevelList(2);
        if (ListUtils.isNotEmpty(categoryList)) {
            for (CategoryVo categoryVo : categoryList) {
                CategoryGoodsVo categoryGoodsVo = new CategoryGoodsVo();
                categoryGoodsVo.setId(categoryVo.getId())
                        .setPid(categoryVo.getPid())
                        .setName(categoryVo.getCategoryName())
                        .setLevel(categoryVo.getTreeLevel());
                result.add(categoryGoodsVo);
            }
        }*/
        // 3级以上分类
        List<CategoryVo> category3List = categoryService.findLevelList(3);
        if (ListUtils.isNotEmpty(category3List)) {
            for (CategoryVo category3Vo : category3List) {
                CategoryGoodsVo categoryGoods3Vo = new CategoryGoodsVo();
                categoryGoods3Vo.setId(category3Vo.getId())
                        .setPid(category3Vo.getPid())
                        .setName(category3Vo.getCategoryName())
                        .setLevel(category3Vo.getTreeLevel())
                        .setImage(category3Vo.getImage());
                result.add(categoryGoods3Vo);
            }
        }
        // 商品
        // 暂时隐藏
        /*IPage<SpuVo> spuVoPage = spuService.findAll(new SpuSearchParam(), 1, 8);
        if (ListUtils.isNotEmpty(spuVoPage.getRecords())) {
            for (SpuVo spuVo : spuVoPage.getRecords()) {
                CategoryGoodsVo categoryGoodsVo = new CategoryGoodsVo();
                categoryGoodsVo.setId(spuVo.getId())
                        .setPid(spuVo.getCategoryId())
                        .setGoodsId(spuVo.getCategoryId())
                        .setName(spuVo.getSpuName())
                        .setPrice(spuVo.getPrice())
                        .setImage(spuVo.getImage())
                        .setLevel(3);
                result.add(categoryGoodsVo);
            }
        }*/
        // 缓存分类商品
        redisService.setCacheList(ApiConstants.CATEGORY_GOODS_KEY, result);
        // 设置缓存时间为5分钟
        redisService.expire(ApiConstants.CATEGORY_GOODS_KEY, 60 * 5);
        return result;
    }

    /**
     * 用户商品记录列表
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SpuVo> viewList(Integer current, Integer pageSize) {
        Page page = new Page();
        UserVo user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        // 商品访问记录KEY
        String key = ApiConstants.GOODS_VIEW_KEY + user.getId();
        // 获取用户的浏览的商品的总数
        long total = redisService.lLen(key);
        //根据用户的ID分頁获取该用户最近浏览的50个商品信息
        List<String> result = redisService.lRange(key, (current - 1) * pageSize, current * pageSize - 1);
        if (result != null && result.size() > 0) {
            List<SpuVo> spuList = new ArrayList<>();
            for (String keys : result) {
                SpuVo spuVo = new SpuVo();
                spuVo.setId(keys.split(",;;")[0]);
                if (keys.split(",;;").length > 1) {
                    spuVo.setImage(keys.split(",;;")[1]);
                }
                spuList.add(spuVo);
            }
            page.setTotal(total);
            page.setRecords(spuList);
        }
        return page;
    }

}
